package com.mate.cloud.xml;

import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * 用于测试命名空间使用的测试工具。
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2024/6/3 16:29
 * @updateUser:     MI
 * @updateTime:     2024/6/3 16:29
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
public class NamespaceTests extends AbstractTestCase {

    /**
     * Input XML file to read
     */
    private static final String INPUT_XML_FILE = "classpath:xml/namespaces.xml";

    /**
     * Namespace to use in tests
     */
    private static final Namespace XSL_NAMESPACE = Namespace.get("xsl", "http://www.w3.org/1999/XSL/Transform");

    private static final QName XSL_TEMPLATE = QName.get("template", XSL_NAMESPACE);

    // Test case(s)
    // -------------------------------------------------------------------------
    @Test
    public void debugShowNamespaces() throws Exception {
        Element root = getRootElement();

        for (Iterator iter = root.elementIterator(); iter.hasNext(); ) {
            Element element = (Element) iter.next();

            log("Found element:    " + element);
            log("Namespace:        " + element.getNamespace());
            log("Namespace prefix: " + element.getNamespacePrefix());
            log("Namespace URI:    " + element.getNamespaceURI());
        }
    }

    @Test
    public void testGetElement() throws Exception {
        Element root = getRootElement();

        Element firstTemplate = root.element(XSL_TEMPLATE);
        assertTrue("Root element contains at least one <xsl:template/> element", firstTemplate != null);

        log("Found element: " + firstTemplate);
    }

    @Test
    public void testGetElements() throws Exception {
        Element root = getRootElement();

        List list = root.elements(XSL_TEMPLATE);
        assertTrue("Root element contains at least one <xsl:template/> element", list.size() > 0);

        log("Found elements: " + list);
    }

    @Test
    public void testElementIterator() throws Exception {
        Element root = getRootElement();
        Iterator iter = root.elementIterator(XSL_TEMPLATE);
        assertTrue("Root element contains at least one <xsl:template/> element", iter.hasNext());

        do {
            Element element = (Element) iter.next();
            log("Found element: " + element);
        } while (iter.hasNext());
    }

    /**
     * Tests the use of namespace URI Mapping associated with a DocumentFactory
     *
     * @throws Exception DOCUMENT ME!
     */
    @Test
    public void testNamespaceUriMap() throws Exception {
        // register namespace prefix->uri mappings with factory
        Map uris = new HashMap();
        uris.put("x", "fooNamespace");
        uris.put("y", "barNamespace");

        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs(uris);
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:xml/test/nestedNamespaces.xml");
        Assert.isTrue(resource.exists(), "XML file not found");
        // parse or create a document
        SAXReader saxReader = SAXReader.createDefault();

        saxReader.setDocumentFactory(factory);

        Document doc = getDocument(resource.getInputStream(), saxReader);


        // evaluate XPath using registered namespace prefixes
        // which do not appear in the document (though the URIs do!)
        String value = doc.valueOf("/x:pizza/y:cheese/x:pepper");

        log("Found value: " + value);

        assertEquals("XPath used default namesapce URIS", "works", value);
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected void setUp() throws Exception {
        super.setUp();
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource(INPUT_XML_FILE);
        Assert.isTrue(resource.exists(), "XML file not found");
        SAXReader saxReader = SAXReader.createDefault();

        document = getDocument(resource.getInputStream(), saxReader);
    }

    /**
     * DOCUMENT ME!
     *
     * @return the root element of the document
     */
    @Override
    protected Element getRootElement() {
        Element root = document.getRootElement();
        assertTrue("Document has root element", root != null);

        return root;
    }

}
